package com.yuanjiu.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import com.yuanjiu.common.util.Global;
import com.yuanjiu.service.jiuxian.JiuxianBrand;
import com.yuanjiu.service.jiuxian.JiuxianData;
import com.yuanjiu.service.w1919.W19Brand;
import com.yuanjiu.service.w1919.W19Data;

public class DataRetrieve {
	
	@Autowired
	private JiuxianBrand jiuxianBrand;
	@Autowired
	private JiuxianData jiuxianData;
	@Autowired
	private W19Brand w19Brand;
	@Autowired
	private W19Data w19data;
	
	Logger logger = Logger.getLogger(DataRetrieve.class);

//	 酒仙数据获取
	public void jiuxian() {
		logger.info("开始抓取酒仙数据");
		String jiuXianUrl = Global.getJiuxianUrl();
		try {
			Document doc = Jsoup.connect(jiuXianUrl).get();
			//使用jsoup抽取页面元素，获取分类链接
			List<String> jiuxianCategoryList=new ArrayList<String>(); 
			Elements cats = doc.select("body > div.navWrap > div > div > ul >li");
			String url1=cats.get(1).select("li>div>h3").attr("url");
			jiuxianCategoryList.add(url1);
			String url2=cats.get(2).select("li>div>h3").attr("url");
			jiuxianCategoryList.add(url2);
			String url3=cats.get(3).select("li>div>h3").attr("url");
			jiuxianCategoryList.add(url3);
			String[] url4 = cats.get(4).select("li>div>h3").attr("url").split("\\|"); 
			jiuxianCategoryList.addAll(Arrays.asList(url4));
			String[] finalUrl=jiuxianCategoryList.toArray(new String[]{});
			
			//品牌获取
			logger.info("正在抓取酒仙网品牌数据...");
			jiuxianBrand.process(finalUrl); 
			//商品数据获取
			logger.info("正在抓取酒仙网商品信息...");
			jiuxianData.process(finalUrl);
//			jiuxianData.process("http://www.jiuxian.com/goods-53691.html");
			logger.info("保存了"+JiuxianData.itemcount+"个商品！");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	// 1919数据获取
	public void w1919(){
		logger.info("开始抓取1919数据...");
		String w19Url=Global.getW19Url();
		try {
			Document doc = Jsoup.connect(w19Url).get();
			//使用jsoup抽取页面元素，获取分类链接
			List<String> w19CategoryList=new ArrayList<String>(); 
			//#mmNav > div > ul > li:nth-child(1) > div.mm-winetp > a
			String url1 = doc.select("#mmNav > div > ul > li:nth-child(1) > div.mm-winetp > a").attr("href");
			w19CategoryList.add(url1);
			String url2=doc.select("#mmNav > div > ul > li:nth-child(2) > div.mm-winetp > a").attr("href");
			w19CategoryList.add(url2);
			String url3=doc.select("#mmNav > div > ul > li:nth-child(3) > p > a").attr("href");
			w19CategoryList.add(url3);
			String url4 = doc.select("#mmNav > div > ul > li:nth-child(4) > div.mm-winetp > a").attr("href");
			w19CategoryList.add(url4);
			String url5=doc.select("#mmNav > div > ul > li:nth-child(5) > div.mm-winetp > div:nth-child(1) > a").attr("href");
			w19CategoryList.add(url5);
			String url6=doc.select("#mmNav > div > ul > li:nth-child(5) > div.mm-winetp > div:nth-child(3) > a").attr("href");
			w19CategoryList.add(url6);
			String[] finalUrl=w19CategoryList.toArray(new String[]{});
			
			//品牌获取
			logger.info("正在抓取1919品牌数据...");
			w19Brand.process(finalUrl);
			//商品数据获取
			logger.info("正在抓取1919商品信息...");
			w19data.process(finalUrl);
//			w19data.process("https://www.1919.cn/p/1004737.html");
			logger.info("保存了"+W19Data.itemcount+"个商品！");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
